package com.hjq.fcloud.widget

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.hjq.fcloud.R
import com.hjq.fcloud.app.AppActivity
import com.hjq.fcloud.bean.FlowInfoItem
import com.hjq.fcloud.bean.FlowSetupList
import com.hjq.fcloud.ktx.ViewKtx.setVisibility
import com.hjq.fcloud.utils.DrawableUtils
import kotlinx.android.synthetic.main.layout_flow_root.view.*


/**
 * 流程 view
 */
class FlowView : LinearLayout {
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        inflateView(attrs)
    }


    private fun inflateView(attrs: AttributeSet?) {
        inflate(context, R.layout.layout_flow_root, this)
    }

    fun setViewData(flowList: List<FlowSetupList>?) {
        if (flowList != null && flowList.size > 0) {
            addFatherView(flowList)
        }
    }

    private fun addFatherView(flowList: List<FlowSetupList>) {
        flowFatherView.removeAllViews()
        for (pos in 0..flowList.size - 1) {
            var it = flowList.get(pos)
            var view = inflate(context, R.layout.layout_flow_father, null)
            flowFatherView.addView(view)

            //最后一个才展示结束view
            var showEnd = (pos == flowList.size - 1)
            view.findViewById<View>(R.id.endView).setVisibility(showEnd)

            addChildFlowView(view, it)
        }
    }

    private fun addChildFlowView(fatherView: View, it: FlowSetupList) {
        var titleTv = fatherView.findViewById<TextView>(R.id.titleTv)
        titleTv.setText(it.title)
        var signTv = fatherView.findViewById<TextView>(R.id.signTv)
        signTv.setText(it.type_name)
        if (it.type.equals("2")) {
            var progress = "(" + it.over + "/" + it.total + ")"
            titleTv.setText(it.title + progress)
            signTv.setVisibility(true)
        } else {
            signTv.setVisibility(false)
            titleTv.setText(it.title)
        }

        var flowChildView = fatherView.findViewById<LinearLayout>(R.id.flowChildView)
        flowChildView.removeAllViews()
        var info: List<FlowInfoItem>? = it.info
        if (info != null) {
            info.forEach {
                var itemView = inflate(context, R.layout.layout_flow_child, null)
                flowChildView.addView(itemView)

                setItemViewData(itemView, it)
            }
        }
    }

    private fun setItemViewData(flowChildView: View, it: FlowInfoItem) {
        flowChildView.findViewById<TextView>(R.id.corpNameTv).setText(it.corp_name)

        var corpTypNameTv = flowChildView.findViewById<TextView>(R.id.corpTypNameTv)
        corpTypNameTv.setText(it.corp_type_name)
        //动态设置背景
        DrawableUtils.setTextStyleByStyle(
            corpTypNameTv,
            it.corp_type_name,
            getCorpTypNameColor(it.corp_type)
        )

        var statusNameTv = flowChildView.findViewById<TextView>(R.id.statusNameTv)
        statusNameTv.setText(it.status_name)
        try {
            statusNameTv.setTextColor(Color.parseColor("#".plus(it.color)))
        }catch (e:Exception){
            e.printStackTrace()
        }

        var dealTimeTv = flowChildView.findViewById<TextView>(R.id.dealTimeTv)
        dealTimeTv.setText(it.deal_time)
        dealTimeTv.setVisibility(!TextUtils.isEmpty(it.deal_time))

        var reasonTv = flowChildView.findViewById<TextView>(R.id.reasonTv)
        reasonTv.setText(it.reason)
        reasonTv.setVisibility(!TextUtils.isEmpty(it.reason))
    }

    private fun getCorpTypNameColor(corpType: String?): String? {
        when (corpType) {
            "1" -> return "#078d5c"
            "2" -> return "#0665bf"
            "3" -> return "#6d6bf0"
            "4" -> return "#d35a21"
            "5" -> return "#9706af"
            "99" -> return "#c9353f"
        }
        return null
    }

}
